package cn.com.tianpeidong.restaurantadmin.project.admin.menu.rest;

import cn.com.tianpeidong.restaurantadmin.common.util.page.PageWrapper;
import cn.com.tianpeidong.restaurantadmin.common.util.response.ServerResponse;
import cn.com.tianpeidong.restaurantadmin.common.web.constant.ProjectConstant;
import cn.com.tianpeidong.restaurantadmin.project.admin.menu.entity.MenuInfo;
import cn.com.tianpeidong.restaurantadmin.project.admin.menu.service.IMenuService;
import cn.com.tianpeidong.restaurantadmin.project.admin.menu.vo.MenuVO;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
import java.net.URI;
import java.util.List;

/**
 * @author tianpeidong
 * @version 创建时间: 19-4-7 下午5:54
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @GetMapping
    @RequiresPermissions("menu:list")
    public ResponseEntity list(
            @RequestParam(value = "per_page", required = false) Integer pageSize,
            @RequestParam(value = "page", required = false) Integer pageNum,
            @RequestParam("pagi") Boolean isPagi) {
        if (!isPagi) {
            return this.getAllMenu();
        }

        PageWrapper pageWrapper = new PageWrapper(pageSize, pageNum);
        List<MenuInfo> list = menuService.listMenu(new MenuInfo());
        return ResponseEntity.ok(pageWrapper.getResult(list));
    }

    @PostMapping("/search")
    @RequiresPermissions("menu:list")
    public ResponseEntity search(
            @RequestParam(value = "per_page", required = false) Integer pageSize,
            @RequestParam(value = "page", required = false) Integer pageNum,
            @RequestBody MenuInfo menuInfo) {
        PageWrapper pageWrapper = new PageWrapper(pageSize, pageNum);
        List<MenuInfo> list = menuService.listMenu(menuInfo);
        return ResponseEntity.ok(pageWrapper.getResult(list));
    }

    @GetMapping("/{id}")
    @RequiresPermissions("menu:list")
    public ResponseEntity getMenuInfoById(@PathVariable("id") Long id) {
        MenuInfo menuInfo = menuService.getMenuById(id);
        return ResponseEntity.ok(menuInfo);
    }

    @PostMapping
    @RequiresPermissions("menu:add")
    public ResponseEntity add(@Valid @RequestBody MenuInfo menuInfo) {
        MenuInfo newMenu = menuService.add(menuInfo);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(newMenu.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }

    @PutMapping
    @RequiresPermissions("menu:edit")
    public ResponseEntity edit(@Valid @RequestBody MenuInfo menuInfo) {
        MenuInfo editMenuInfo = menuService.edit(menuInfo);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(editMenuInfo.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }

    @DeleteMapping("/{menuId}")
    @RequiresPermissions("menu:delete")
    public ResponseEntity delete(@PathVariable("menuId") Long menuId) {
        return menuService.delete(menuId) == ProjectConstant.SUCCESS ?
                new ResponseEntity(HttpStatus.NO_CONTENT) : new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    private ResponseEntity getAllMenu() {
        return ResponseEntity.ok(menuService.listAllMenu());
    }
}
